package com.tac.service.impl;

import com.tac.common.result.Result;
import com.tac.common.thread.Thread;
import com.tac.dao.UserAddressDao;
import com.tac.pojo.UserAddress;
import com.tac.service.UserAddressService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class UserAddressImpl implements UserAddressService {
    @Autowired
    private UserAddressDao userAddressDao;

    /**
     * 添加地址
     * @param userAddress
     * @return
     */
    public Result save(UserAddress userAddress){
        userAddress.setCreateUser(Thread.getId());
        Integer save = userAddressDao.save(userAddress);
        if(save>0){
         return Result.success(null,"success");
        }
        return Result.error("添加失败");
    }

    /**
     * 删除地址
     * @param id
     * @param id
     * @return
     */
    public Result remove( Integer id){
        Integer remove = userAddressDao.remove(Thread.getId(), id);
        if(remove>0){
            return Result.success(null,"success");
        }
        return Result.error("删除失败");
    }


    /**
     * 按照用户id和地址id修改地址
     * @param userAddress
     * @return
     */
    public Result updeteByid(UserAddress userAddress){
        userAddress.setUpdateTime(new Date(System.currentTimeMillis()));
        userAddress.setCreateUser(Thread.getId());
        Integer integer = userAddressDao.updeteByid(userAddress);
        if(integer>0){
            return Result.success(null,"success");
        }
        return Result.error("修改失败");
    }
    /**
     * 按照用户id查询用户地址
     * @param
     * @return
     */
    public Result<List<UserAddress>> selectAll(){
        List<UserAddress> userAddresses = userAddressDao.selectAll(Thread.getId());
        return Result.success(userAddresses,"success");
    }

    /**
     * 设置用户默认地址
     * @return
     */
   public Result updateStaus(Integer id){
       userAddressDao.updeteByUid(Thread.getId());
        UserAddress userAddress = userAddressDao.selectById(id);
        userAddress.setDefaultFlag(1);
        userAddressDao.updeteByid(userAddress);
        return Result.success(null,"success");
    }
    /**
     * 按照id查询用户地址
     * @param id
     * @return
     */
    public Result<UserAddress> selectById(Integer id){
        UserAddress userAddress = userAddressDao.selectById(id);
        return Result.success(userAddress,"success");
    }
}
